//
//  Data.swift
//  SwiftUiBaseStudy
//
//  Created by Daniel on 2023/11/7.
//

import SwiftUI

struct Post:Identifiable,Codable {
    var id :Int
    // let id = UUID()
    var userId:Int
    var title:String
    var body:String
    
}

class Api{
    func getPosts(completion:@escaping ([Post])->()){
        guard let url = URL(string: "https://jsonplaceholder.typicode.com/posts") else { return }
        URLSession.shared.dataTask(with: url) { data,_,_ in
            let posts = try! JSONDecoder().decode([Post].self, from: data!)
            DispatchQueue.main.async {
                completion(posts)
            }
        }
        .resume()
    }
}
